/*
 * SavePromptDialog.java
 *
 * Created on November 6, 2006, 5:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.framework.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 *
 * @author grant
 */
public class SavePromptDialog extends Dialog
{
    /** User wants to close the document without saving. */
    public final static int DONT_SAVE = 0;
    /** User wants to save the document and close it. */
    public final static int SAVE = 1;
    /** User wants to cancel the close. */
    public final static int CANCEL = 2;
    
    private int result;
    /** Creates a new instance of SavePromptDialog */
    public SavePromptDialog(Shell parent)
    {
        super(parent, SWT.PRIMARY_MODAL);
    }
    
    /** 
     * Prompt the user to save.
     * @param documentName The name of the document to ask whether or not to 
     * save.
     * @return One of the constants DONT_SAVE, SAVE, or CANCEL.
     * @see #DONT_SAVE
     * @see #SAVE
     * @see #CANCEL
     */
    public int promptToSave(String documentName)
    {
        Shell parent = getParent();
        final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
        shell.setText("");
        
        // Create a prompt to save...
        GridLayout layout = new GridLayout(3, false);
        layout.marginWidth = 20;
        layout.marginHeight = 20;
        layout.horizontalSpacing = 8;
        shell.setLayout(layout);
        
        Label mainPrompt = new Label(shell, SWT.LEFT);
        mainPrompt.setText("Do you want to save changes to \"" + documentName + "\" before closing?");
        Font existingFont = mainPrompt.getFont();
        FontData fontData = existingFont.getFontData()[0];
        fontData.setHeight(13);
        fontData.setStyle(SWT.BOLD);
        mainPrompt.setFont(new Font(parent.getDisplay(), fontData));
        GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1);
        mainPrompt.setLayoutData(data);
        
        Label details = new Label(shell, SWT.LEFT);
        details.setText("If you don't save, your changes will be lost.");
        fontData.setHeight(11);
        fontData.setStyle(SWT.NORMAL);
        details.setFont(new Font(parent.getDisplay(), fontData));
        data = new GridData(SWT.FILL, SWT.BEGINNING, true, true, 3, 1);
        details.setLayoutData(data);
        
        Button dontSave = new Button(shell, SWT.PUSH);
        dontSave.setText("Don't Save");
        data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false);
        dontSave.setLayoutData(data);
        dontSave.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                result = DONT_SAVE;
                shell.close();
            }
        });
        
        Button cancel = new Button(shell, SWT.PUSH);
        cancel.setText("Cancel");
        data = new GridData(SWT.TRAIL, SWT.BEGINNING, false, false);
        data.horizontalIndent = 20;
        cancel.setLayoutData(data);
        cancel.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                result = CANCEL;
                shell.close();
            }
        });
        
        Button save = new Button(shell, SWT.PUSH);
        save.setText("Cancel"); // part of a hack to get the stupid button to be the same size as the cancel button
        data = new GridData(SWT.FILL, SWT.BEGINNING, false, false);
        Point cancelSize = cancel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        data.minimumWidth = cancelSize.x; //GridLayout seems to ignore (or perhaps overwrite?) this attribute.
        save.setLayoutData(data);
        shell.pack(); // force the layout data to be computed with the button's label "Cancel"
        save.setText("Save"); // now switch back to Save - it doesn't recompute the size.
        shell.setDefaultButton(save);
        save.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                result = SAVE;
                shell.close();
            }
        });
        
        shell.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                if (e.stateMask == SWT.MOD1)
                {
                    switch(e.character)
                    {
                        case 'd': // trigger don't save
                            result = DONT_SAVE;
                            shell.close();
                            break;
                        case 's': // trigger save
                            result = SAVE;
                            shell.close();
                            break;
                    }
                }
                if (e.keyCode == SWT.ESC) { // trigger cancel
                    result = CANCEL;
                    shell.close();
                }
            }
        });
        
        shell.setSize(474, 156);
        shell.open();
        Display display = parent.getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        return result;
    }
}
